require "rubygame"
include Rubygame

raise 'Warning, images disabled' unless 
  ($image_ok = (VERSIONS[:sdl_image] != nil))
raise 'Warning, font disabled' unless 
  ($font_ok = (VERSIONS[:sdl_ttf] != nil))
raise 'Warning, sound disabled' unless
  ($sound_ok = (VERSIONS[:sdl_mixer] != nil))

# Get the resources directory relative to script dir
RESOURCES_DIR = File.join(File.dirname(__FILE__),'..','assets')

# Set the directories to autoload images and sounds from.
# See the docs for Rubygame::NamedResource.
Surface.autoload_dirs = [ RESOURCES_DIR ]
Sound.autoload_dirs = [ RESOURCES_DIR ]

def main
	
	# Initialize Everything
	Rubygame.init()
	screen = Screen.new([800, 600], 0, [RESIZABLE])
	screen.title = 'Cellwars'
	#screen.show_cursor = false;
	queue = EventQueue.new()

	# Create The Background
	background = Surface.new(screen.size)
	background.fill([250,250,250])

	#Init fonts
	Rubygame::TTF.setup()

	font = TTF.new(File.join(RESOURCES_DIR,"FreeSans.ttf"),16)
	text = font.render("Pummel The Chimp, And Win $$$", true, [10,10,10])
	textpos = text.make_rect()
	textpos.centerx = background.width/2
	text.blit(background,textpos)

	background.blit(screen, [0,0])
	screen.update()
	
	count = 10
	size = 36
	grid = Surface.new([count*size+1,count*size+1`])
	grid.fill([200,200,200])
	(count+1).times do |n|
		offset = n * size
		grid.draw_line_a([offset,0],[offset,count*size],[10,10,10])		
		grid.draw_line_a([0,offset],[count*size, offset],[10,10,10])		
	end
	grid.blit(background, [100,100])
	
	
	clock = Clock.new { |clock| clock.target_framerate = 30 }

	whiff_sound = Sound['whiff.wav']
	punch_sound = Sound['punch.wav']
	
	#chimp = Chimp.new()
	#fist = Fist.new()
	
	allsprites = Sprites::Group.new()
	#allsprites.push(chimp, fist)
	
	#Main Loop
	loop do
		clock.tick()
		
		queue.each do |event|
			case(event)
				
				when QuitEvent
					return
				when KeyDownEvent
					case event.key 
					when K_ESCAPE
						return
					end					
				when MouseDownEvent
					punch_sound.play
				when ResizeEvent
					Screen.set_mode(event.size, 0, [RESIZABLE])
			end
		end

		allsprites.update()
		
		background.blit(screen, [0, 0])
		allsprites.draw(screen)
		screen.update()

		screen.title = '%d'%clock.framerate
	end

ensure
	Rubygame.quit()
end


#this calls the 'main' function when this script is executed
if $0 == __FILE__
	main()
end
